package com.atguigu.eduService.service.impl;

import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.*;
import com.atguigu.eduService.mapper.*;
import com.atguigu.eduService.service.EduCourseDescriptionService;
import com.atguigu.eduService.service.EduCourseService;
import com.atguigu.eduService.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-07-22
 */
@Service
@Transactional
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;
    @Resource
    private EduCourseMapper eduCourseMapper;
    @Resource
    private EduVideoMapper eduVideoMapper;
    @Resource
    private EduChapterMapper eduChapterMapper;
    @Resource
    private VodClient vodClient;
    @Resource
    private EduTeacherMapper eduTeacherMapper;

    //添加基本课程信息
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int count = eduCourseMapper.insert(eduCourse);
        if (count == 0) {
            throw new RuntimeException();
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionMapper.insert(eduCourseDescription);
        return eduCourse.getId();
    }

    //根据课程id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = eduCourseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        EduCourseDescription eduCourseDescription = eduCourseDescriptionMapper.selectById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        eduCourseMapper.updateById(eduCourse);
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescriptionMapper.updateById(eduCourseDescription);
    }

    //根据课程id获取课程确认信息
    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return eduCourseMapper.getCoursePublishInfo(id);
    }

    //课程最终发布
    @Override
    public void publishCourse(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseMapper.updateById(eduCourse);
    }

    //分页条件查询课程
    @Override
    public IPage<EduCourse> getCoursePageList(Long current, Long size, CourseQueryVo courseQueryVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        Page<EduCourse> page = new Page<>();
        if (StringUtils.isEmpty(courseQueryVo)) {
            return eduCourseMapper.selectPage(page, queryWrapper);
        }
        if (!StringUtils.isEmpty(courseQueryVo.getTitle())) {
            queryWrapper.like("title", courseQueryVo.getTitle());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getTeacherId())) {
            queryWrapper.eq("teacher_id", courseQueryVo.getTeacherId());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQueryVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQueryVo.getSubjectId());
        }
        return eduCourseMapper.selectPage(page, queryWrapper);
    }

    //删除课程
    @Override
    public void deleteCourse(String courseId) {
        //删除视频
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoMapper.selectList(queryWrapper);
        List<String> videoIds = new ArrayList<>();
        eduVideos.forEach(e->{
            if (!StringUtils.isEmpty(e.getVideoSourceId())) {
                videoIds.add(e.getVideoSourceId());
            }
        });
        if (videoIds.size() > 0) {
            vodClient.deleteBatch(videoIds);
        }
        //删除小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",courseId);
        eduVideoMapper.delete(eduVideoQueryWrapper);
        //删除章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        eduChapterMapper.delete(chapterQueryWrapper);
        //删除描述
        eduCourseDescriptionMapper.deleteById(courseId);
        //删除课程
        eduCourseMapper.deleteById(courseId);
    }

    @Cacheable(value = "index", key = "'courseAndTeacher'")
    @Override
    public Map<String, Object> index() {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.orderByDesc("view_count");
        eduCourseQueryWrapper.last("limit 8");
        List<EduCourse> eduCourseList = eduCourseMapper.selectList(eduCourseQueryWrapper);
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.orderByDesc("id");
        eduTeacherQueryWrapper.last("limit 4");
        List<EduTeacher> eduTeacherList = eduTeacherMapper.selectList(eduTeacherQueryWrapper);
        map.put("eduCourseList", eduCourseList);
        map.put("eduTeacherList", eduTeacherList);
        return map;
    }

    //查询课程列表带分页
    @Override
    public Map<String, Object> getCourseFrontList(Long page, Long limit, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            eduCourseQueryWrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            eduCourseQueryWrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            eduCourseQueryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            eduCourseQueryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            eduCourseQueryWrapper.orderByAsc("price");
        }
        Page<EduCourse> coursePage = new Page<>(page, limit);
        eduCourseMapper.selectPage(coursePage,eduCourseQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("items", coursePage.getRecords());
        map.put("current", coursePage.getCurrent());
        map.put("pages", coursePage.getPages());
        map.put("size", coursePage.getSize());
        map.put("total", coursePage.getTotal());
        map.put("hasNext", coursePage.hasNext());
        map.put("hasPrevious", coursePage.hasPrevious());
        return map;
    }

    //查询课程详情
    @Override
    public CourseWebVo getCourseFrontInfo(String courseId) {
        return baseMapper.getCourseFrontInfo(courseId);
    }
}
